/**
 * 模板素材模块
 */
/* eslint-disable no-undef */
define( [
  'jquery',
  'require',
  '../../modules/createEditor',
  '../../modules/templatePreview',
  './templateLogic',
  'api',
  'bigwe_utils',
  'message',
  'selector'
], function( $, require ) {

  var returnParam = {};

  var msg = require( 'message' );
  var template = require( './templateLogic' );
  var tp = require( '../../modules/templatePreview' );
  var BIGWE_API = require( 'api' );
  var ue = require( '../../modules/createEditor' );


  var showTime = msg.showTime;
  var $typeset = $( '.typeset_div' );
  // 模板数据初始化
  returnParam.init = function() {
    if ( !typeset[ 0 ].isInit ) {
      typeset[ 0 ].ajaxRequest();
    }
  };

  $( '.selectpicker', $typeset ).selectpicker();

  $( '.title_explain .iconfont', $typeset ).popover( {
    html: true,
    content: '① 在编辑器中粘贴或者输入所有内容；<br/>② 点击对应的一键排版规则；<br/>③ 全文内容实现自动排版；',
    trigger: 'hover',
    container: 'body'
  } );

  $( '[data-toggle="tooltip"]', $typeset ).popover( {
    html: true,
    container: 'body',
    trigger: 'hover'
  } );

  var typeset = [ {
    param: {
      p: 1
    },
    ajaxRequest: function( option ) {
      this.isLoading = !0;
      $.get( BIGWE_API.SYSTEM_TEMPLATE.GET_TYPESE_TEMPLATE, $.extend( {}, this.param, option ), $.proxy( function( data ) {
        this.isLoading = 0;
        this.isInit = !0;
        data = $.parseJSON( data );
        if ( data.ret === 0 ) {
          for ( var i in data.data ) {
            var temData = data.data[ i ];
            temData.title = temData.typeset_mould_name;
            var $templateItem = $( this.htmlRander( {
              pic: temData.typeset_mould_img,
              title: temData.typeset_mould_name
            }, 'typeset' ) ).data( temData ).prop( 'title', '点击秒排版' );
            $( '.typeset_wrapper .spinner', $typeset ).before( $templateItem ).hide();
          }
          $( '.js_template_prev', $typeset ).tooltip( {
            container: 'body'
          } );
        }
      }, this ) );
    },
    htmlRander: template.htmlBuild
  } ];

  $typeset.on( 'click', '.js_template_prev', function( e ) {
    e.stopPropagation();

    tp.show( $( this ).parents( '.fw-details' ).data( 'title' ) );

    var $typeset_item = $( this ).parents( '.fw-details' );
    var typesetData = $typeset_item.data();

    $.ajax( {
      type: 'post',
      url: BIGWE_API.SYSTEM_TEMPLATE.GET_TYPESE_TEMPLATE_BY_ID,
      data: {
        id: typesetData.id
      },
      success: function( data ) {
        data = typeof data === 'string' ? $.parseJSON( data ) : data;

        tp.contentRander( {
          'tid': typesetData.id,
          'tempType': $typeset_item.attr( 'temp_type' ),
          'content': data.data.typeset_mould
        } );
      }
    } );
  } ).on( 'click', '.fw-details', function() {

    var typesetData = $( this ).data();
    var $typeset_frm = $( '#typeset_param' );
    $( '#typeset_id', $typeset_frm ).val( typesetData.id );
    $( '#typeset_content', $typeset_frm ).val( ue.getContent() );
    $.ajax( {
      type: 'post',
      url: BIGWE_API.SYSTEM_TEMPLATE.GET_TEMPLATE_TYPESETTING,
      data: $typeset_frm.serialize(),
      success: function( data ) {
        data = typeof data === 'string' ? $.parseJSON( data ) : data;

        if ( data.ret === 0 ) {
          ue.undoManger.save();
          ue.setContent( data.data );
          ue.undoManger.save();
        }
      }
    } );
  } ).on( 'change', '#is_recognition', function() {
    if ( $( this ).val() === '0' ) {
      $( '.dynamic_param', $typeset ).addClass( 'hide' ).find( '.dynamic_disabled' ).prop( 'disabled', 'disabled' );
      $( '.typeset_wrapper ', $typeset ).transit( {
        top: 98
      } );

    } else {
      $( '.dynamic_param', $typeset ).removeClass( 'hide' ).find( '.dynamic_disabled' ).removeAttr( 'disabled' );
      $( '.typeset_wrapper ', $typeset ).transit( {
        top: ''
      } );
    }
  } );

  /*$( '.formwork-own', $typeset ).scrollMax( function() {
  	var myTemplate = typeset[ 0 ];
  	if ( !myTemplate.isLoading && !myTemplate.isNodata ) {
  		myTemplate.ajaxRequest( {
  			p: ++myTemplate.param.p
  		} );
  	}
  } );*/



  return returnParam;
} );